/**
 * ModuleListController.java
 */
package org.yate.oa.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.math.NumberUtils;
import org.yate.oa.model.ModuleEntity;
import org.yate.oa.service.IModuleService;
import org.yate.oa.service.imp.ModuleService;
import org.yate.oa.utils.Pagination;

import com.alibaba.fastjson.JSONObject;

/**
 * @author Yate
 * @date Nov 18, 2013
 * @description TODO
 * @version 1.0
 */
@WebServlet(urlPatterns = {"/module/get","/module/post"}, asyncSupported = true)
public class ModuleController extends RestfulController
{
    
    private IModuleService modService;
    
    /*
     * (non-Javadoc)
     * 
     * @see org.yate.oa.controller.BaseController#init(javax.servlet.ServletConfig)
     */
    @Override
    public void init(ServletConfig config)
        throws ServletException
    {
        super.init(config);
        
        this.modService = (IModuleService)super.springCtx.getBean("moduleService");
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest ,
     * javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
    {
        int rows = NumberUtils.toInt(req.getParameter("rows"), 0);
        int page = NumberUtils.toInt(req.getParameter("page"), Integer.MAX_VALUE);
        
        Pagination<ModuleEntity> d = this.modService.getEntities(rows, page, "", "");
        
        req.setAttribute("data", d);
        req.getRequestDispatcher("/jsp/modules/list.jsp").forward(req, resp);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        String type = req.getParameter("type");
        if (type.equals("add"))
        {
            addModel(req, res);
        }else if (type.equals("all"))
        {
            getModels(req, res);
        }
    }
    
    protected void addModel(HttpServletRequest req, HttpServletResponse res)
        throws IOException
    {
        String modName = req.getParameter("modName");
        PrintWriter out = res.getWriter();
        JSONObject json = new JSONObject();
        ModuleService moduleService = this.springCtx.getBean(ModuleService.class);
        ModuleEntity moduleEntity = new ModuleEntity();
        moduleEntity.setModName(modName);
        moduleEntity = moduleService.getEntityBy(moduleEntity);
        if (moduleEntity == null)
        {
            moduleEntity = new ModuleEntity();
            moduleEntity.setModName(modName);
            moduleEntity.setModUrl("");
            moduleService.add(moduleEntity);
            json.put("status", 200);
        }
        else
        {
            json.put("status", 201);
            json.put("text", "模块名已经存在");
        }
        out.println(json);
        out.flush();
        out.close();
    }
    
    protected void getModels(HttpServletRequest req, HttpServletResponse res)
        throws IOException
    {
        PrintWriter out = res.getWriter();
        JSONObject json = new JSONObject();
        ModuleService moduleService = this.springCtx.getBean(ModuleService.class);
        List<ModuleEntity> list = moduleService.getEntitiesAll();
        json.put("data", list);
        json.put("status", 200);
        out.println(json);
        out.flush();
        out.close();
    }
}
